/**
 * AI回复语音选择测试工具
 * 在浏览器控制台中使用
 */

// AI回复语音选择测试工具
window.aiVoiceTest = {
    // 测试AI回复是否使用选中的语音
    async testAiVoiceSelection() {
        console.log('🎤 测试AI回复语音选择...');

        const testVoices = [
            { id: 'xiaoyan', name: '小燕', text: '你好，我是小燕，温柔女声。' },
            { id: 'aisjiuxu', name: '艾斯', text: '你好，我是艾斯，御姐音。' },
            { id: 'xiaorong_sichuan', name: '四川话', text: '你好，我是四川话，这是我的声音效果。' },
            { id: 'xiaokun_henan', name: '河南话', text: '你好，我是河南话，这是我的声音效果。' }
        ];

        for (let i = 0; i < testVoices.length; i++) {
            const voice = testVoices[i];
            console.log(`🎤 测试 ${voice.name} (${i + 1}/${testVoices.length})`);

            try {
                if (window.voiceChatManager) {
                    // 选择语音
                    window.voiceChatManager.setVoiceCharacter(voice.id);

                    // 检查TTS参数是否正确更新
                    const params = window.voiceChatManager.ttsParams;
                    console.log(`📋 ${voice.name} TTS参数:`, params);

                    // 模拟AI回复
                    const aiResponse = `这是AI回复，应该使用${voice.name}的声音`;
                    console.log(`🤖 模拟AI回复: ${aiResponse}`);

                    // 停止当前播放
                    window.voiceChatManager.stopSpeaking();

                    // 等待一下
                    await new Promise(resolve => setTimeout(resolve, 500));

                    // 使用TTS服务播放AI回复
                    if (window.ttsService) {
                        await window.ttsService.speak(aiResponse, params);
                        console.log(`✅ ${voice.name} AI回复播放完成`);
                    } else {
                        console.error('❌ ttsService不存在');
                    }
                } else {
                    console.error('❌ voiceChatManager不存在');
                }

                // 等待播放完成
                await new Promise(resolve => setTimeout(resolve, 3000));

            } catch (error) {
                console.error(`❌ ${voice.name} 测试失败:`, error);
            }
        }

        console.log('🎤 AI回复语音选择测试完成');
    },

    // 测试单个语音的AI回复
    async testSingleAiVoice(voiceId, aiText) {
        console.log(`🎤 测试AI回复语音: ${voiceId}`);

        try {
            if (window.voiceChatManager) {
                // 选择语音
                window.voiceChatManager.setVoiceCharacter(voiceId);

                // 检查TTS参数
                const params = window.voiceChatManager.ttsParams;
                console.log(`📋 TTS参数:`, params);

                // 模拟AI回复
                console.log(`🤖 模拟AI回复: ${aiText}`);

                // 停止当前播放
                window.voiceChatManager.stopSpeaking();

                // 等待一下
                await new Promise(resolve => setTimeout(resolve, 500));

                // 使用TTS服务播放AI回复
                if (window.ttsService) {
                    await window.ttsService.speak(aiText, params);
                    console.log(`✅ ${voiceId} AI回复播放完成`);
                } else {
                    console.error('❌ ttsService不存在');
                }
            } else {
                console.error('❌ voiceChatManager不存在');
            }
        } catch (error) {
            console.error(`❌ ${voiceId} 测试失败:`, error);
        }
    },

    // 检查当前AI回复语音设置
    checkAiVoiceSettings() {
        console.log('🔍 检查当前AI回复语音设置...');

        if (window.voiceChatManager) {
            const currentCharacter = window.voiceChatManager.getCurrentVoiceCharacter();
            if (currentCharacter) {
                console.log('📋 当前AI回复语音角色:');
                console.log('  - ID:', currentCharacter.id);
                console.log('  - 名称:', currentCharacter.name);
                console.log('  - 描述:', currentCharacter.description);
                console.log('  - VCN:', currentCharacter.vcn);
                console.log('  - 分类:', currentCharacter.category);
            } else {
                console.log('❌ 未设置AI回复语音角色');
            }

            const params = window.voiceChatManager.ttsParams;
            console.log('📋 当前AI回复TTS参数:');
            console.log('  - vcn:', params.vcn);
            console.log('  - speed:', params.speed);
            console.log('  - volume:', params.volume);
            console.log('  - pitch:', params.pitch);
        } else {
            console.error('❌ voiceChatManager不存在');
        }
    },

    // 模拟完整的AI对话流程
    async simulateAiConversation(voiceId, userMessage) {
        console.log(`🎤 模拟AI对话流程: ${voiceId}`);

        try {
            if (window.voiceChatManager) {
                // 选择语音
                window.voiceChatManager.setVoiceCharacter(voiceId);

                // 检查TTS参数
                const params = window.voiceChatManager.ttsParams;
                console.log(`📋 选择的语音TTS参数:`, params);

                // 模拟用户输入
                console.log(`👤 用户输入: ${userMessage}`);

                // 模拟AI回复
                const aiResponse = `这是AI对"${userMessage}"的回复，应该使用选中的语音`;
                console.log(`🤖 AI回复: ${aiResponse}`);

                // 停止当前播放
                window.voiceChatManager.stopSpeaking();

                // 等待一下
                await new Promise(resolve => setTimeout(resolve, 500));

                // 使用TTS服务播放AI回复
                if (window.ttsService) {
                    await window.ttsService.speak(aiResponse, params);
                    console.log(`✅ AI对话模拟完成`);
                } else {
                    console.error('❌ ttsService不存在');
                }
            } else {
                console.error('❌ voiceChatManager不存在');
            }
        } catch (error) {
            console.error(`❌ AI对话模拟失败:`, error);
        }
    }
};

// 快速测试函数
window.testAiVoice = async function () {
    console.log('🎤 快速测试AI回复语音选择...');
    await window.aiVoiceTest.testAiVoiceSelection();
};

window.testSingleAi = async function (voiceId, aiText) {
    console.log(`🎤 快速测试单个AI回复语音: ${voiceId}`);
    await window.aiVoiceTest.testSingleAiVoice(voiceId, aiText);
};

window.checkAiVoice = function () {
    console.log('🔍 检查AI回复语音设置...');
    window.aiVoiceTest.checkAiVoiceSettings();
};

window.simulateAi = async function (voiceId, userMessage) {
    console.log(`🎤 快速模拟AI对话: ${voiceId}`);
    await window.aiVoiceTest.simulateAiConversation(voiceId, userMessage);
};

console.log('🎤 AI回复语音选择测试工具已加载！');
console.log('📖 使用方法:');
console.log('  - testAiVoice() - 测试所有AI回复语音选择');
console.log('  - testSingleAi("xiaoyan", "你好") - 测试指定AI回复语音');
console.log('  - checkAiVoice() - 检查AI回复语音设置');
console.log('  - simulateAi("xiaorong_sichuan", "你好") - 模拟AI对话');
